package com.jiangxiaofei.citycloud.mvp;

import android.content.Context;
import android.widget.Toast;

import com.jiangxiaofei.citycloud.bean.DoWorkInfoBean;
import com.jiangxiaofei.citycloud.bean.JobBean;
import com.jiangxiaofei.citycloud.bean.JobListBean;
import com.jiangxiaofei.citycloud.bean.MapWorkBean;
import com.jiangxiaofei.citycloud.bean.MapWorkerBean;
import com.jiangxiaofei.citycloud.bean.UserInfoBean;
import com.jiangxiaofei.citycloud.bean.WorkLabelBean;
import com.jiangxiaofei.citycloud.bean.WorkNumBean;
import com.jiangxiaofei.citycloud.httpUtil.HttpResult;
import com.jiangxiaofei.citycloud.httpUtil.OnLoadDataIm;
import com.jiangxiaofei.citycloud.httpUtil.RetrofitUtils;
import com.jiangxiaofei.citycloud.param.AddJobsParam;
import com.jiangxiaofei.citycloud.param.RecruitDoWorkParam;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/9/11 0011.
 */

public class RecruitModel implements RecruitConstant.UserInfoModel, RecruitConstant.WorkLabelModel
        , RecruitConstant.DoWorkModel, RecruitConstant.DoWorkInfoModel, RecruitConstant.AddjobInfoModel
        , RecruitConstant.UpdateJobModel, RecruitConstant.GetJobListModel, RecruitConstant.GetJobInfoModel
        , RecruitConstant.GetWorkerModel, RecruitConstant.GetWorkModel, RecruitConstant.UpdateWorkerInfoModel
        , RecruitConstant.DeleteWorkModel, RecruitConstant.getWorkerNumModel, RecruitConstant.getWorkNumModel {
    static final int client = 1;//1：Android   0：IOS

    //登陆获取用户信息
    @Override
    public void getUserInfo(final Context mContext, String name, String password, final OnLoadDataIm callBack) {
        RetrofitUtils.getApiService(mContext)
                .getUser(name, password)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<UserInfoBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<UserInfoBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callBack.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, listHttpResult.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    public void getWorkLabel(Context mContext, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext).getWorkLabel()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<WorkLabelBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<WorkLabelBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    @Override
    public void postDoWork(final Context mContext, int id, String introduce, String coverage, String lat, String log, String role, String freetime, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .postDowork(id, introduce, coverage, lat, log, role, freetime)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, stringHttpResult.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    public void getDoWorkInfo(final Context mContext, int userId, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext).getDoWorkInfo(userId, client)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<DoWorkInfoBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<DoWorkInfoBean>> doWorkInfoBeanHttpResult) {
                        if (doWorkInfoBeanHttpResult.getCode() == 1) {
                            callback.onSuccess(doWorkInfoBeanHttpResult.getData(), doWorkInfoBeanHttpResult.getMessage(), doWorkInfoBeanHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, "请求网络失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    //添加招聘信息
    @Override
    public void addJobInfo(final Context mContext, int userId, String role, String workspace, String workTime, String other, int pay, String phone, String contacts, String lat, String log, String description, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .addJobs(userId, role, workspace, workTime, other, pay, phone, contacts, lat, log, description)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        } else {
                            Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    //修改招聘信息
    @Override
    public void updateJob(Context mContext, AddJobsParam param, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext).updateJobs(param.getId(), param.getUserId(),
                param.getRole(), param.getWorkspace(),
                param.getWorkTime(), param.getOther(),
                param.getPay(), param.getPhone(), param.getContacts(),
                param.getLat(), param.getLog(), param.getDescription())
                .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<HttpResult<String>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult<String> stringHttpResult) {
                if (stringHttpResult.getCode() == 1) {
                    callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                }
            }
        });
    }

    //    获取工作列表
    @Override
    public void getJobList(Context mContext, int userId, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getJobList(userId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<JobListBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<JobListBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    //    获取工作详情
    @Override
    public void getJobInfo(Context mContext, int id, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getJobInfo(id,client)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<JobBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<JobBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    //获取工作坐标
    @Override
    public void getWork(Context mContext, int role, final OnLoadDataIm callback) {
        if (role == 0) {
            RetrofitUtils.getApiService(mContext)
                    .getMapWorkAll()
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<HttpResult<List<MapWorkBean>>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(HttpResult<List<MapWorkBean>> listHttpResult) {
                            if (listHttpResult.getCode() == 1) {
                                callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                            }
                        }
                    });
        } else {
            RetrofitUtils.getApiService(mContext)
                    .getMapWork(role)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<HttpResult<List<MapWorkBean>>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            callback.onFailure("请求失败", 0);
                        }

                        @Override
                        public void onNext(HttpResult<List<MapWorkBean>> listHttpResult) {
                            if (listHttpResult.getCode() == 1) {
                                callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                            } else {
                                callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                            }
                        }
                    });
        }
    }

    //获取干活坐标
    @Override
    public void getWorker(Context mContext, int role, final OnLoadDataIm callback) {
        if (role == 0) {
            RetrofitUtils.getApiService(mContext)
                    .getMapWorkerAll()
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<HttpResult<List<MapWorkerBean>>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(HttpResult<List<MapWorkerBean>> listHttpResult) {
                            if (listHttpResult.getCode() == 1) {
                                callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                            }
                        }
                    });
        } else {
            RetrofitUtils.getApiService(mContext)
                    .getMapWorker(role)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<HttpResult<List<MapWorkerBean>>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            callback.onFailure("请求失败", 0);
                        }

                        @Override
                        public void onNext(HttpResult<List<MapWorkerBean>> listHttpResult) {
                            if (listHttpResult.getCode() == 1) {
                                callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                            } else {
                                callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                            }
                        }
                    });
        }
    }

    //更新工人信息
    @Override
    public void updateWorkerInfo(Context mContext, RecruitDoWorkParam param, final OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .updateWorkerInfo(param.getId(), param.getIntroduce(),
                        param.getCoverage(), param.getLat(), param.getLog(),
                        param.getRole(), param.getFreetime())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        }
                    }
                });

    }

    //删除工作
    @Override
    public void deleteWork(Context mContext, String id, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .deleteWorkList(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callback.onSuccess(stringHttpResult.getData(), stringHttpResult.getCode());
                        }
                    }
                });
    }

    //获取工人数量
    @Override
    public void getWorkerNum(Context mContext, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getWorkerNum(client)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<WorkNumBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<WorkNumBean>> workNumBeanHttpResult) {
                        if (workNumBeanHttpResult.getCode() == 1) {
                            callback.onSuccess(workNumBeanHttpResult.getData(), workNumBeanHttpResult.getMessage(), workNumBeanHttpResult.getCode());
                        }
                    }
                });
    }

    //获取工作数量
    @Override
    public void getWorkNum(Context mContext, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getWorkNum(client)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<WorkNumBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<WorkNumBean>> workNumBeanHttpResult) {
                        if (workNumBeanHttpResult.getCode() == 1) {
                            callback.onSuccess(workNumBeanHttpResult.getData(), workNumBeanHttpResult.getMessage(), workNumBeanHttpResult.getCode());
                        }
                    }
                });
    }
}
